package com.ysd.lis.service.smart.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.slef.SelfMachine;
import com.ysd.lis.entity.smart.SmartScreen;
import com.ysd.lis.mapper.self.SelfMachineMapper;
import com.ysd.lis.mapper.smart.SmartScreenMapper;
import com.ysd.lis.request.FeeItemPageParam;
import com.ysd.lis.service.smart.SmartScreenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wh
 * @since 2025-05-12
 */
@Service
public class SmartScreenServiceImpl extends ServiceImpl<SmartScreenMapper, SmartScreen> implements SmartScreenService {

    @Autowired
    SmartScreenMapper smartScreenMapper;
    @Override
    public Result findSmartScreenList(FeeItemPageParam feeItemPageParam) {

        LambdaQueryWrapper<SmartScreen> queryWrapper=new LambdaQueryWrapper<>();

        String keywords = feeItemPageParam.getKeyWords();
        if (StrUtil.isNotEmpty(keywords)) {
            queryWrapper.and(wrapper -> wrapper.like(SmartScreen::getSmartCode, keywords).or().like(SmartScreen::getSmartName, keywords));
        }
        queryWrapper.eq(SmartScreen::getDelFlag,0);

        List<SmartScreen> smartScreenList = smartScreenMapper.selectList(queryWrapper);
        return Result.succ(smartScreenList);
    }

    @Override
    public Result savaOrUpdateSmartScreen(SmartScreen smartScreen) {
        LambdaQueryWrapper<SmartScreen> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SmartScreen::getSuid,smartScreen.getSuid())
                .eq(SmartScreen::getDelFlag,0);

        boolean b = this.saveOrUpdate(smartScreen,queryWrapper);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }
    }

    @Override
    public Result deleteSmartScreen(List<SmartScreen> smartScreenList) {
        List<String> ids = smartScreenList.stream().map(SmartScreen::getId).filter(StringUtils::isNotBlank).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail("删除失败");
        }    }

    @Override
    public Result uploadSelfImage(String id, Map<String, String> map) {

        SmartScreen smartScreen = new SmartScreen();
        smartScreen.setId(id);
        smartScreen.setImagePath(map.get("path"));
        smartScreen.setImageName(map.get("name"));
        this.updateById(smartScreen);
        return Result.succ(1, "上传成功", smartScreen);
    }

    @Override
    public Result findSmartScreenBySuid(String suid) {
        if (StrUtil.isEmpty(suid)){
            return Result.fail("suid为空");
        }
        LambdaQueryWrapper<SmartScreen> queryWrapper =  new LambdaQueryWrapper<>();
        queryWrapper.eq(SmartScreen::getSuid,suid)
                .eq(SmartScreen::getDelFlag,0);
        SmartScreen smartScreen = smartScreenMapper.selectOne(queryWrapper);

        // 必须做null判断
        if (smartScreen == null) {
            // 处理不存在的情况
            return Result.fail("未找到该设备");
        }else {
            return Result.succ(smartScreen);
        }

    }

    @Override
    public Result findSmartScreens(FeeItemPageParam feeItemPageParam) {
        Map<String,Object> returnMap = new HashMap<>();

        List<SmartScreen> windowList = smartScreenMapper
                .selectList(new LambdaQueryWrapper<SmartScreen>().eq(SmartScreen::getDelFlag, 0));
        returnMap.put("allCnt",windowList.size());
        //查询未启用的数量
        Integer noWindowCnt = Math.toIntExact(windowList.stream().filter(a -> !"1".equals(a.getStatus())).count());
        returnMap.put("runCnt",noWindowCnt);


        return Result.succ(1, "查询成功", returnMap);
    }
}
